home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Audio / Patchmix / Source / Reson.m < prev    next >
Text File  |  1992-08-09  |  6KB  |  249 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "Reson.h"
  5. #import "Instrum.h"
  6. #import <appkit/graphics.h>
  7. #import "Statement.h"
  8.  
  9. @implementation Reson
  10.  
  11. + initialize
  12. {
  13.        image = [NXImage findImageNamed:"Reson"];
  14.     cfOffset.x = 3;
  15.     cfOffset.y = 30;
  16.     inOffset.x = 30;
  17.     inOffset.y = 51;
  18.     bwOffset.x = 52;
  19.     bwOffset.y = 30;
  20.     outOffset.x = 29;
  21.     outOffset.y = 3;
  22.     
  23.     return self;
  24. }
  25.  
  26. - init
  27. {
  28.     written = NO;
  29.     size.width = 80.;
  30.     size.height = 80.;
  31.     center.x = size.width/2.;
  32.     center.y = size.height/2.;
  33.     strcpy(type,"Reson");
  34.     strcpy(name,"RESON");    // cmix macro
  35.     
  36.     cf = [[Param alloc] init:self :&cfOffset];
  37.     in = [[Param alloc] init:self :&inOffset];
  38.     bw = [[Param alloc] init:self :&bwOffset];
  39.     out = [[Param alloc] init:self :&outOffset];
  40.  
  41.     [cf setValue:"440"];    // set non-"1" default values
  42.     [bw setValue:"20"];    
  43.     [cf setTitle:"CFreq:"];
  44.     [bw setTitle:"BWid"];
  45.     [in setTitle:"Input:"];
  46.     [out setTitle:"Out:"];
  47.         
  48.     paramList = [[List alloc] initCount:5];
  49.     [paramList addObject:cf];
  50.     [paramList addObject:in];
  51.     [paramList addObject:bw];
  52.     [paramList addObject:out];
  53.     
  54.     [Inst putUgenInList:self];
  55.     
  56.     return self;
  57. }
  58.  
  59. - remove
  60. {
  61.     id cp,param;
  62.     int i;
  63.                             // make sure not connected first 
  64.     for(i = 0; i < ([paramList count]); i++) {    
  65.         param = [paramList objectAt:i];
  66.         if(cp = [param getConnectedParam]) 
  67.             return 0;
  68.     }
  69.                             // remove ugen from list
  70.     [Inst removeUgenFromList:self];
  71.     return self;
  72. }
  73.  
  74. - (NXImage *)getImage
  75. {
  76.     return image;
  77. }
  78.  
  79. - move:(NXPoint *)newloc
  80. {
  81.     location = *newloc;
  82.     [cf move:&location];
  83.     [in move:&location];
  84.     [bw move:&location];
  85.     [out move:&location];
  86.     return self;
  87. }
  88.  
  89. - findParamAtPoint:(NXPoint *)point
  90. {
  91.     NXRect *rect;
  92.     int i;
  93.     id param;
  94.     
  95.     for(i = 0; i < [paramList count]; i++) {
  96.         param = [paramList objectAt:i];
  97.         rect = [param getRect];
  98.         if(NXMouseInRect(point,rect,NO))
  99.             return param;
  100.     }
  101.     //printf("no param found\n");
  102.     return nil;
  103. }
  104.  
  105. - writeUgen
  106. {
  107.     BOOL ai = NO;    // amplitude input ugen
  108.     BOOL ri = NO;    // rise input ugen
  109.     BOOL di = NO;    // decay input ugen
  110.     char ampVar[10];
  111.     char par[20];
  112.     /*     for amp, rise, & decay input params:
  113.             see if there is input ugen
  114.                 if so, grab the output (ug?) and multiply it by this
  115.                     param value
  116.                 if not just use the param value
  117.          after all params done, write relevant code into lists
  118.             (declarations, assignments and loop statements)
  119.             i.e. ug? = oscil(,,,,);
  120.         go to output param connected ugen and call its "writeUgen"
  121.     */
  122.     int i;
  123.     int parent[4];
  124.     id param;
  125.     id ug;
  126.     id nupar;
  127.     char str[50];
  128.     
  129.     //printf("writing ugen %d\n",index);
  130.     sprintf(str,"\tfloat ug%d;\n",index);
  131.     [Inst putVarInList:str];
  132.     sprintf(str,"\tfloat amp%d;\n",index);
  133.     [Inst putVarInList:str];
  134.     sprintf(str,"\tfloat cf%d;\n",index);
  135.     [Inst putVarInList:str];
  136.     sprintf(str,"\tfloat in%d;\n",index);
  137.     [Inst putVarInList:str];
  138.     sprintf(str,"\tfloat bw%d;\n",index);
  139.     [Inst putVarInList:str];
  140.     sprintf(str,"\tfloat scl%d;\n",index);
  141.     [Inst putVarInList:str];
  142.     sprintf(str,"\tfloat xinit%d;\n",index);
  143.     [Inst putVarInList:str];
  144.     sprintf(str,"\tfloat val%d;\n",index);
  145.     [Inst putVarInList:str];
  146.     sprintf(str,"\tfloat rsnetc%d[5];\n",index);
  147.     [Inst putVarInList:str];
  148.     sprintf(str,"\tfloat out%d;\n",index);
  149.     [Inst putVarInList:str];
  150.  
  151.     for(i = 0; i < ([paramList count]-1); i++) {    
  152.         param = [paramList objectAt:i];
  153.         if(nupar = [param getConnectedParam]) {
  154.             ug = [nupar getUgen];
  155.             if([ug getWritten] == NO) { 
  156.                 parent[i] = [[ug writeUgen] getIndex];
  157.             }
  158.             else  {
  159.                 parent[i] = 0;
  160.             }
  161.             //printf("parent[%d] = %d\n",i,parent[i]);
  162.         }
  163.         else 
  164.             parent[i] = 0;
  165.         
  166.     }
  167.     
  168.     strcpy(par,[[paramList objectAt:0] getValue]);
  169.     if(par[0] == 'p' && par[1] == '[')
  170.         sprintf(str,"\tcf%d = %s;\n", index, par);
  171.     else
  172.         sprintf(str,"\tcf%d = %.2f;\n", index, atof(par));
  173.     [Inst putAssignInList:str];
  174.     
  175.     strcpy(par,[[paramList objectAt:1] getValue]);
  176.     if(par[0] == 'p' && par[1] == '[')
  177.         sprintf(str,"\tamp%d = %s;\n", index, par);
  178.     else
  179.         sprintf(str,"\tamp%d = %.2f;\n", index, atof(par));
  180.     [Inst putAssignInList:str];
  181.     
  182.     strcpy(par,[[paramList objectAt:2] getValue]);
  183.     if(par[0] == 'p' && par[1] == '[')
  184.         sprintf(str,"\tbw%d = %s;\n", index, par);
  185.     else
  186.         sprintf(str,"\tbw%d = %.2f;\n", index, atof(par));
  187.     [Inst putAssignInList:str];
  188.     
  189.  
  190.     if(parent[1]) {     //  input ugen    
  191.         //printf("input ugen to reson\n");
  192.         ai = YES;
  193.         sprintf(str,"\tin%d = %.2f;\n",index, 
  194.             atof([[paramList objectAt:1] getValue]));
  195.         [Inst putAssignInList:str];
  196.         sprintf(str,"\tfloat inai%d;\n",index);
  197.         [Inst putVarInList:str];
  198.         sprintf(str,"\t\tinai%d = amp%d * ug%d;\n",index,  
  199.             index,parent[1]);
  200.         [Inst putLoopInList:str];
  201.     }
  202.     else {                // input preset or default
  203.         //printf("input preset or default\n");
  204.          strcpy(par,[[paramList objectAt:0] getValue]);
  205.         if(par[0] == 'p' && par[1] == '[')
  206.             sprintf(str,"\tamp%d = %s;\n", index, par);
  207.         else
  208.             sprintf(str,"\tamp%d = %.2f;\n",index,atof(par));
  209.         [Inst putAssignInList:str];
  210.     }
  211.     if(ai)
  212.         sprintf(ampVar,"inai%d",index);
  213.     else
  214.         sprintf(ampVar,"amp%d",index);
  215.         
  216.     if(!strcmp([[[in getConnectedParam] getUgen] getType],"Rand")) 
  217.         sprintf(str,"\tscl%d = 2;\n", index, par);
  218.     else
  219.         sprintf(str,"\tscl%d = 1;\n", index, par);
  220.     [Inst putAssignInList:str];
  221.     sprintf(str,"\txinit%d = 0;\n", index, par);
  222.     [Inst putAssignInList:str];
  223.     sprintf(str,"\trsnetc%d[3] = rsnetc%d[4] = 0;\n", index, index);
  224.     [Inst putAssignInList:str];
  225.          
  226.                         //     set up reson filter
  227.     sprintf(str, "\trsnset(cf%d,bw%d,scl%d,xinit%d,rsnetc%d);\n",index,index,index, index,index);
  228.     [Inst putAssignInList:str];
  229.     
  230.                     // output amplitude multiplier:
  231.     strcpy(par,[[paramList objectAt:3] getValue]);
  232.     if(par[0] == 'p' && par[1] == '[')
  233.         sprintf(str,"\tout%d = %s;\n",index,par);
  234.     else
  235.         sprintf(str,"\tout%d = %.2f;\n", index, atof([[paramList objectAt:3] getValue]));
  236.     [Inst putAssignInList:str];
  237.  
  238.     sprintf(str,"\t\t%s(val%d,%s,rsnetc%d);\n", name, index, ampVar, index);
  239.     [Inst putLoopInList:str];
  240.     sprintf(str,"\t\tug%d = val%d*out%d;\n", index, index, index);
  241.     [Inst putLoopInList:str];
  242.     
  243.     written = YES;
  244.     return self;    
  245. }
  246.  
  247.  
  248. @end
  249.